//
//  STNativeCollectionViewController
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STNativeCollectionViewController: STBaseViewController {
    
    private let viewModel = NativeCollectionViewModel()
    
    /* management */
    private lazy var management: UICollectionViewManagement = {
        let view = UICollectionViewManagement(viewModel)
        return view
    }()
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewLeftFlowLayout()
        layout.scrollDirection = .vertical
        
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.scrollsToTop = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        view.register(cellClass: NativeAdCellModel.registerCell)
        view.dataSource = management
        view.delegate = management
        
        return view
    }()
    
    deinit {
        print("-->toj: class: \(cm_objectName), func: \(#function)")
    }
}

// MARK: - 生命周期
extension STNativeCollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        
        collectionView.reloadData()
    }
}

// MARK: - 点击事件
@objc private extension STNativeCollectionViewController {
    
}

// MARK: - Private
private extension STNativeCollectionViewController {
    
}

// MARK: - 初始化设置
private extension STNativeCollectionViewController {
    
    func setupMakeAddSubviews() {
        
        view.addSubview(collectionView)
    }
    
    func setupMakeLayoutSubviews() {
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(CGFloat.nav_barFullHeight)
        }
    }
    
    func setupMakeInitialize() {
        viewModel.requestNativeAds()
    }
}


class UICollectionViewLeftFlowLayout: UICollectionViewFlowLayout {
    
    weak var delegate: UICollectionViewDelegateFlowLayout?
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        let layoutAttributess = super.layoutAttributesForElements(in: rect)
        var maxY: CGFloat = 0
        layoutAttributess?.forEach({ attributes in
            if maxY == attributes.frame.maxY { return }
            
            //print("-->toj: native: x: \(attributes.frame.origin.x), edgeInsets:\(edgeInsets)")
            
            
            let edgeInsets = sectionInsetForItemAtIndex(section: attributes.indexPath.section)
            var frame = attributes.frame
            frame.origin.x = edgeInsets.left
            attributes.frame = frame
            maxY = frame.maxY
        })

        return layoutAttributess
    }
    func sectionInsetForItemAtIndex(section: Int) -> UIEdgeInsets {
        guard let collectionView = collectionView else { return sectionInset }
        
         let layoutDelegate = collectionView.delegate as? UICollectionViewDelegateFlowLayout
        
        let edgeInsets = layoutDelegate?.collectionView?(collectionView, layout: self, insetForSectionAt: section) ?? sectionInset
        
        return edgeInsets
    }
}
